﻿using Messhall.Domain.Permission.IRepositorys;
using Messhall.Framework.Autofac;
using Messhall.Framework.Domain.DomainObject;

namespace Messhall.Domain.Permission.BusinessObjects;

public class PermissionCodeEntity : EntityBoBase
{
    #region 成员变量

    /// <summary>
    /// 权限Id
    /// </summary>
    public Guid PermissionKeyId { get; set; }

    /// <summary>
    /// 权限唯一CODE代码
    /// </summary>
    public string Code { get; set; }

    /// <summary>
    /// 权限名称
    /// </summary>
    public string Name { get; set; }
    
    /// <summary>
    /// 权限
    /// </summary>
    public virtual PermissionEntity Permission{ get; set; }

    #endregion
}

public class PermissionCode : BoBase, IAggregateRoot
{
    private PermissionCodeEntity SelfEntity
    {
        get { return Entity as PermissionCodeEntity; }
        set { this.Entity = value; }
    }

    public PermissionCode(Guid permissionKeyId, string name, string code)
    {
        this.SelfEntity = new PermissionCodeEntity()
        {
            PermissionKeyId = permissionKeyId,
            Code = code,
            Name = name
        };
    }


    public PermissionCode(PermissionCodeEntity entity)
    {
        this.SelfEntity = entity;
    }

    public Permission _permission;

    public async Task<Permission> GetPermission()
    {
        if (_permission == null)
        {
            var permissionRepository = AutofacBuilder.Resolve<IPermissionRepository>();
            _permission = await permissionRepository.FindAsync(this.GetPermissionKeyId());
        }

        return _permission;
    }

    public Guid GetPermissionKeyId()
    {
        return this.SelfEntity.PermissionKeyId;
    }

    public void SetPermissionKeyId(Guid permissionKeyId)
    {
        this.SelfEntity.PermissionKeyId = permissionKeyId;
    }

    public string GetCode()
    {
        return this.SelfEntity.Code;
    }

    public void SetCode(string code)
    {
        this.SelfEntity.Code = code;
    }

    public string GetName()
    {
        return this.SelfEntity.Name;
    }

    public void SetName(string name)
    {
        this.SelfEntity.Name = name;
    }
}